﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.RoboticOperationMerge.CApROMObserverManager
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using System;
using System.Collections;
using System.Collections.ObjectModel;
using Tecnomatix.Engineering;


namespace DnProcessSimulateCommands.RoboticOperationMerge;

internal class CApROMObserverManager
{
  private ArrayList m_reggedObjects;

  public event ObservedObjectChangedEventHandler OnObservedObjectChanged;

  public CApROMObserverManager() => this.m_reggedObjects = new ArrayList();

  public bool RegisterObject(ITxObject obj)
  {
    bool flag = true;
    try
    {
      if (obj != null)
      {
        if (!this.m_reggedObjects.Contains((object) obj))
        {
          obj.Deleted += new TxObject_DeletedEventHandler(this.obj_Deleted);
          obj.NameChanged += new TxObject_NameChangedEventHandler(this.obj_NameChanged);
          if (obj is ITxObjectCollection objectCollection1)
            objectCollection1.ItemsRemoved += new TxObjectCollection_ItemsRemovedEventHandler(this.compObj_ItemsRemoved);
          if (obj is ITxOrderedObjectCollection objectCollection2)
            objectCollection2.ChildMoved += new TxOrderedObjectCollection_ChildMovedEventHandler(this.orderedCompObj_ChildMoved);
          this.m_reggedObjects.Add((object) obj);
        }
      }
    }
    catch (Exception ex)
    {
      flag = false;
    }
    return flag;
  }

  public bool UnRegisterObject(ITxObject obj)
  {
    bool flag = true;
    try
    {
      if (obj != null)
      {
        if (this.m_reggedObjects.Contains((object) obj))
        {
          obj.Deleted -= new TxObject_DeletedEventHandler(this.obj_Deleted);
          obj.NameChanged -= new TxObject_NameChangedEventHandler(this.obj_NameChanged);
          if (obj is ITxObjectCollection objectCollection1)
            objectCollection1.ItemsRemoved -= new TxObjectCollection_ItemsRemovedEventHandler(this.compObj_ItemsRemoved);
          if (obj is ITxOrderedObjectCollection objectCollection2)
            objectCollection2.ChildMoved -= new TxOrderedObjectCollection_ChildMovedEventHandler(this.orderedCompObj_ChildMoved);
          this.m_reggedObjects.Remove((object) obj);
        }
      }
    }
    catch (Exception ex)
    {
      flag = false;
    }
    return flag;
  }

  public void UnRegisterObservedObjects()
  {
    try
    {
      for (int index = this.m_reggedObjects.Count - 1; index >= 0; --index)
        this.UnRegisterObject(this.m_reggedObjects[index] as ITxObject);
    }
    catch (Exception ex)
    {
    }
    this.m_reggedObjects.Clear();
  }

  private void NotifyChange(
    ITxObject nodeObject,
    ObservedObjectAction action,
    TxObjectList relatedObjects)
  {
    if (this.OnObservedObjectChanged == null)
      return;
    this.OnObservedObjectChanged((object) this, new ObservedObjectChangedEventArgs(nodeObject, action, relatedObjects));
  }

  private void obj_Deleted(object sender, TxObject_DeletedEventArgs e)
  {
    if (!(sender is ITxObject nodeObject))
      return;
    this.NotifyChange(nodeObject, ObservedObjectAction.Deleted, new TxObjectList());
  }

  private void obj_NameChanged(object sender, TxObject_NameChangedEventArgs e)
  {
    this.NotifyChange(sender as ITxObject, ObservedObjectAction.NameChanged, new TxObjectList());
  }

  private void compObj_ItemsRemoved(object sender, TxObjectCollection_ItemsRemovedEventArgs e)
  {
    try
    {
      foreach (ITxObject itxObject in (Collection<ITxObject>) e.Items)
        this.UnRegisterObject(itxObject);
    }
    catch (Exception ex)
    {
    }
    this.NotifyChange(sender as ITxObject, ObservedObjectAction.ItemsRemoved, e.Items);
  }

  private void orderedCompObj_ChildMoved(
    object sender,
    TxOrderedObjectCollection_ChildMovedEventArgs e)
  {
    this.NotifyChange(sender as ITxObject, ObservedObjectAction.ChildMoved, new TxObjectList());
  }
}
